/*  File Name	: Connector.h
 *  Author		: Pang Jiacai
 *	 E-mail		: 823960168@qq.com
 *  Created on	: 2012-6-25
 *  Description:
 */

#ifndef CONNECTOR_H_
#define CONNECTOR_H_
#include "Reactor.h"
#include <map>
#include <sys/socket.h>

//template<class SVC_HANDLER>
//class Connector_Base
//{
//public:
//	Connector_Base()
//	{
//	}
//	virtual ~Connector_Base(void)
//	{
//	}
//
//	//Initialize the Svc_Handler.
//	//virtual int init_svc_handler(int handle, SVC_HANDLER *svc_handler) = 0;
//
//	//Return the handle set representing the non-blocking connects in progress.
//	virtual std::map<int,int> &non_blocking_handles(void) = 0;
//};
//
//template<class SVC_HANDLER>
//class NonBlock_Handle_Connector:virtual public EventHandler
//{
//public:
//	NonBlock_Handle_Connector(Connector_Base<SVC_HANDLER> &connector,
//			SVC_HANDLER *svc,long timer_id);
//
//	virtual ~NonBlock_Handle_Connector();
//
//	bool close(SVC_HANDLER *svc);
//
//	SVC_HANDLER *svc_handler(void);
//
//	int handle(void) const;
//
//	long timer_id(void);
//
//	void timer_id(long timer_id);
//
//	//Called by ACE_Reactor when asynchronous connections fail.
//	virtual int handle_input(int handle);
//
//	//Called by ACE_Reactor when asynchronous connections succeed.
//	virtual int handle_output(int handle);
//
//	//Called by ACE_Reactor when asynchronous connections suceeds (on
//	//some platforms only).
//	virtual int handle_exception(int handle);
//
//	//This method is called if a connection times out before
//	//completing.
//	virtual int handle_timeout(void);
//
//private:
//
//	Connector_Base<SVC_HANDLER> connector_;
//
//	SVC_HANDLER *svc_handler_;
//
//	long time_id_;
//};

template<class SVC_HANDLER,class PEER_CONNECTOR>
class Connector:virtual public EventHandler
{
public:
	typedef PEER_CONNECTOR	ConnectorType;
	typedef SVC_HANDLER		SvcType;

	Connector(Reactor *reactor=NULL,bool non_block=true);

	virtual ~Connector();

	virtual const PEER_CONNECTOR &connector(void) ;

	virtual int init_svc_hanlder(int handle,SVC_HANDLER *svc);

	virtual int open(Reactor *reactor,bool non_block);

	virtual int connect(std::string ip,int port,SVC_HANDLER *&svc=NULL,
			TimeValue timeout=TimeValue::zero,int pro_family=AF_INET,
			int type=SOCK_STREAM,int protocol=0,bool reuseaddr=true);

	virtual int close(void);

	virtual void reactor(Reactor *reactor);

	virtual Reactor *reactor (void) const;

	SVC_HANDLER *svc(void);

	void svc(SVC_HANDLER *s);

	void set_peer(std::string ip,int port);

protected:
	//typedef NonBlock_Handle_Connector<SVC_HANDLER> NBHC;

	virtual int make_svc_handler(SVC_HANDLER *&svc);

	virtual int connect_svc_handler(SVC_HANDLER *svc,std::string ip,int port,
			TimeValue &timeout,int pro_family,int type,int protocol,bool reuseaddr);

	virtual int active_svc_handler(SVC_HANDLER *svc);

	virtual std::map<int,int> &non_blocking_handles(void);

private:
	PEER_CONNECTOR connector_;

	SVC_HANDLER *svc_;

	bool non_block_;

	Reactor *reactor_;

	std::map<int,int> non_blocking_handles_;

	bool new_svc_;

	std::string ip_;

	int port_;
};

#include "Connector.cpp"

#endif /* CONNECTOR_H_ */
